package com.xiaoyu.client.ui.activity.mall;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.xiaoyu.client.ClientConstants;
import com.xiaoyu.client.R;
import com.xiaoyu.client.ui.activity.help.HelpDetailActivity;
import com.xiaoyu.client.ui.fragment.main.MainActivity;
import com.xiaoyu.client.ui.fragment.mall.goods.GoodsCommentFragment;
import com.xiaoyu.client.ui.fragment.mall.goods.GoodsDetailFragment;
import com.xiaoyu.commonlib.ui.base.BaseActivity;
import com.xiaoyu.commonlib.utils.CommonFragmentPagerAdapter;
import com.xiaoyu.commonlib.utils.ToastUtil;
import com.xiaoyu.commonlib.utils.UIUtils;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 商品详情页面
 */
public class GoodsDetailsActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.activity_goods_detail_function_layout)
    LinearLayout functionLayout;//功能容器
    @BindView(R.id.activity_goods_detail_function_line)
    View mLine;//标志线
    @BindView(R.id.activity_goods_detail_view_page)
    ViewPager mViewPager;
    @BindView(R.id.activity_goods_detail_title_back_btn)
    ImageView backBtn;//返回
    @BindView(R.id.activity_goods_detail_title_share_btn)
    ImageView shareBtn;//分享

    private CommonFragmentPagerAdapter mPagerAdapter;
    private List<Fragment> mFragmentList = new ArrayList<>();
    private String goodsId;//商品id

    //功能标示线
    private float buttonWidth;//每一个切换title控件的宽度


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_details);
        ButterKnife.bind(this);
        goodsId = getIntent().getStringExtra(ClientConstants.GOODS_ID);
        Log.i("xiaoyuu","goods id = "+goodsId);
        initView();
    }

    private void initView() {
        //商品详情fragment
        GoodsDetailFragment detailFragment = new GoodsDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putString(ClientConstants.GOODS_ID, goodsId);
        detailFragment.setArguments(bundle);

        //评价fragment
        GoodsCommentFragment commentFragment = new GoodsCommentFragment();
        Bundle bundle1 = new Bundle();
        bundle1.putString(ClientConstants.GOODS_ID, goodsId);
        commentFragment.setArguments(bundle1);
        mFragmentList.clear();
        mFragmentList.add(detailFragment);
        mFragmentList.add(commentFragment);

        //viewpager
        mPagerAdapter = new CommonFragmentPagerAdapter(getSupportFragmentManager(), mFragmentList);
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.addOnPageChangeListener(onPageChangeListener);

        //功能项、标识线
        initLine();

        for (int i = 0; i < functionLayout.getChildCount(); i++) {
            functionLayout.getChildAt(i).setOnClickListener(this);
        }
        backBtn.setOnClickListener(this);
        shareBtn.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        if (v == functionLayout.getChildAt(0)) {//商品详情
            selectItem(0);
            scrollLineMove(0, 0);
            mViewPager.setCurrentItem(0);
        } else if (v == functionLayout.getChildAt(1)) {//累计评价
            selectItem(1);
            scrollLineMove(1, 0);
            mViewPager.setCurrentItem(1);
        } else if (v == backBtn) {//返回
            onBackPressed();
        } else if (v == shareBtn) {//分享
            if (!UMShareAPI.get(this).isInstall(this, SHARE_MEDIA.WEIXIN)){
                ToastUtil.showBelowToast(this,"您还未安装微信");
                return;
            }
            toShare();
        }
    }


    /**
     * viewpager 滑动监听
     */
    private ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            scrollLineMove(position, positionOffset);
        }

        @Override
        public void onPageSelected(int position) {
            selectItem(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    public void initLine() {
        buttonWidth = functionLayout.getWidth() / functionLayout.getChildCount();
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mLine.getLayoutParams();
        params.width = UIUtils.dip2px(50);
        params.leftMargin = (int) ((buttonWidth - params.width) / 2 + functionLayout.getLeft());
        mLine.setLayoutParams(params);

        Log.i("jiangxue", "func = " + functionLayout.getWidth() + "   left = " + functionLayout.getLeft()
                + "   button = " + buttonWidth);
    }


    /**
     * 设置功能标志线位置
     *
     * @param position
     */
    public void scrollLineMove(int position, float offset) {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mLine.getLayoutParams();
        int leftMargin = (int) ((buttonWidth - params.width) / 2 + buttonWidth * (position + offset) + functionLayout.getLeft());
        params.leftMargin = leftMargin;
        mLine.setLayoutParams(params);
    }

    /**
     * 设置功能titlebar选项的文字颜色
     *
     * @param position
     */
    public void selectItem(int position) {
        for (int i = 0; i < functionLayout.getChildCount(); i++) {
            TextView view = (TextView) functionLayout.getChildAt(i);
            if (i == position) {
                view.setTextColor(Color.parseColor("#2796f0"));
            } else {
                view.setTextColor(Color.parseColor("#333333"));
            }
        }
    }


    /**
     * 发送分享
     */
    private void toShare(){
        UMImage image = new UMImage(this, R.drawable.share_logo);//网络图片
        image.compressStyle = UMImage.CompressStyle.SCALE;//大小压缩，默认为大小压缩，适合普通很大的图
        image.compressStyle = UMImage.CompressStyle.QUALITY;//质量压缩，适合长图的分享
        //压缩格式设置
        image.compressFormat = Bitmap.CompressFormat.PNG;//用户分享透明背景的图片可以设置这种方式，但是qq好友，微信朋友圈，不支持透明背景图片，会变成黑色

        UMWeb web = new UMWeb(ClientConstants.Share_Goods_Url+goodsId);
        web.setTitle("你身边的卡车之家");//标题
        web.setThumb(image);  //缩略图
        web.setDescription("汽车、汽车配件、汽车用品的销售及网上销售，汽车配件的修理服务，车辆信息咨询服务，商务信息咨询服务，广告设计、制作、代理、发布。（依法须经批准的项目，经相关部门批");//描述

        new ShareAction(this).withMedia(web).
                setDisplayList(SHARE_MEDIA.WEIXIN,SHARE_MEDIA.WEIXIN_CIRCLE,SHARE_MEDIA.QQ,SHARE_MEDIA.SINA)
                .setCallback(shareListener).open();
    }

    /**
     * 友盟分享回调
     */
    private UMShareListener shareListener = new UMShareListener() {
        /**
         * @descrption 分享开始的回调
         * @param platform 平台类型
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
            ToastUtil.showBelowToast(GoodsDetailsActivity.this,"分享成功");
        }

        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Log.i("xiaoyuu","分享失败  原因："+t.toString());
            ToastUtil.showBelowToast(GoodsDetailsActivity.this,"分享失败");
        }

        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
            ToastUtil.showBelowToast(GoodsDetailsActivity.this,"取消了分享");
        }
    };

}
